import axios from "axios";
import router from "../router";
const request = axios.create({
  baseURL: '/api',
  timeout: 5000
})
// 当我们发起http请求的时候需要添加拦截器
request.interceptors.request.use(config =>{
  config.headers['Content-Type'] = "application/json;charset=UTF-8";
  config.headers.Authorization = LocalStorage.get(LOCAL_KEY_XINGUAN_ACCESS_TOKEN)
    return config;
},  error =>{
  console.log("请求拦截器报错："+error)
  return Promise.reject(error)
})
request.interceptors.response.use(response =>{
  let res = response.data
  if(response.config.responseType === 'blob'){
    return res;
  }
  if(typeof res === 'string'){
    res = res ?JSON.parse(res):res
  }
  if(res.success) return response ;
  if(res.data !=null && res.data.errorCaptured === 50001){
    LocalStorage.clearAll()
    return router.push('/login')
  }
  return response;
},error => {
  console.log("系统错误:"+error)
  return Promise.reject(error)
})
export default request;
